<?php
class JChat
{
    public function register($arrUserUids)
    {
        $url  = 'https://api.im.jpush.cn/v1/users';
        $data = array();

        foreach ($arrUserUids as $uid)
        {
            $data[] = array(
                'username' => 'user'.$uid,
                'password' => 'youzi',
            );
        }
            
        $result = $this->request($url, $data);
        return $result;
    }

    public function getUserInfo ($uid)
    {
        $username = 'user'.$uid;
        $url      = 'https://api.im.jpush.cn/v1/users/'.$username;
        $result   = $this->request($url, array(), 'GET');
        return $result;
    }

    public function setUserAvatar($uid)
    {
        $iconUrl = 'http://cps.yingyonghui.com/cps/www/images/logo.png';
        $username = 'user'.$uid;
        $url  = 'https://api.im.jpush.cn/v1/users/'.$username;
        $data = array(
            'avatar' => $iconUrl,
        );

        $result = $this->request($url, $data, 'PUT');
        return $result;
    }

    public function setUserInfo ($uid, $nickname, $gender)
    {
        $username = 'user'.$uid;
        $url  = 'https://api.im.jpush.cn/v1/users/'.$username;
        $data = array(
            'nickname' => $nickname,
            'gender'   => $gender == 'f' ? 2 : 1,
        );

        $result = $this->request($url, $data, 'PUT');
        return $result;
    }

    public function setAlias ($regId, $alias)
    {
        $url  = 'https://device.jpush.cn/v3/devices/' . rawurlencode($regId);
        $data = array(
            'alias' => $alias,
        );

        $result = $this->post($url, $data);
        return $result;
    }

    public function setTags ($regId, $arrAddTags = NULL, $arrDelTags = NULL)
    {
        $url  = 'https://device.jpush.cn/v3/devices/' . rawurlencode($regId);
        $tags = array();

        if (! empty($arrAddTags))
            $tags['add'] = $arrAddTags;
        if (! empty($arrDelTags))
            $tags['remove'] = $arrDelTags;

        if (empty($tags))
            return FALSE;

        $data = array(
            'tags' => $tags,
        );

        $result = $this->post($url, $data);
        return $result;
    }

    public function getAlias ($regId)
    {
        $url  = 'https://device.jpush.cn/v3/devices/'.rawurlencode($regId);
        $result = $this->get($url);
        return $result;
    }

    private function get ($url) 
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, FALSE);

        $base64Auth = $this->getAuthString();
        $headerArr = array("Authorization: Basic $base64Auth");
        curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr );

        $json_info = curl_exec($ch);

        if (curl_errno($ch)) {
            Kohana::$log->add(LOG::ERROR, "CURL_ERROR:".curl_error($ch));
            return NULL;
        }

        curl_close($ch);

        $result = @json_decode($json_info, TRUE);
        return $result;

    }


    public function request ($url, $array_param, $method = 'POST') 
    {
        $post_str_data = json_encode($array_param);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, FALSE);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str_data);

        $base64Auth = $this->getAuthString();
        $headerArr = array(
            "Authorization: Basic $base64Auth",
            "Content-type: application/json",
        );
        curl_setopt ($ch, CURLOPT_HTTPHEADER , $headerArr );

        $json_info = curl_exec($ch);

        if (curl_errno($ch)) {
            Kohana::$log->add(LOG::ERROR, "CURL_ERROR:".curl_error($ch));
            return NULL;
        }

        //$info = curl_getinfo($ch);

        curl_close($ch);

        $result = @json_decode($json_info, TRUE);
        return $result;

    }


    private function getAuthString ()
    {
        $appKey = Kohana::config('jpush.appKey');
        $appSec = Kohana::config('jpush.masterSecret');

        return base64_encode("$appKey:$appSec");
    }
}

